﻿Imports HTLibrary
Imports TNCBAL
Imports TNCDAL

Public Class frmChangeConnectDB
    Inherits DevComponents.DotNetBar.Office2007Form

    Private Sub frmChangeConnectDB_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            'For Each p As String In CollectionHelper.GetSqlServerList()
            '    cmbServer.Items.Add(p)
            'Next
            cmbServer.Text = GeneralClassBAL.GetGiaTriThanhPhan("Data Source")
            LoadDatabase()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

    End Sub

    Private Sub LoadDatabase()
        Try
            If cmbServer.Text <> "" Then
                cmbDatabase.Items.Clear()
                Dim lst = CollectionHelper.GetDatabases(cmbServer.Text)
                For Each p As String In lst
                    cmbDatabase.Items.Add(p)
                Next

                If cmbDatabase.Items.Count > 0 Then
                    If cmbDatabase.Items.Contains("TanNamChinh") Then
                        cmbDatabase.Text = "TanNamChinh"
                    Else
                        cmbDatabase.Text = cmbDatabase.Items(0)
                    End If
                End If
            End If
        Catch ex As Exception
            Throw ex
        End Try
        
    End Sub

    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        Try
            If cmbServer.Text = "" Then
                MessageBox.Show("Server name is not empty!")
                Return
            End If

            If cmbDatabase.Text = "" Then
                MessageBox.Show("Database name is not empty!")
                Return
            End If

            If txtUsername.Text = "" Then
                MessageBox.Show("User Id is not empty!")
                Return
            End If

            Dim result = GeneralClassDAL.ChangeConnectString(cmbServer.Text, cmbDatabase.Text, txtUsername.Text, txtPassword.Text)
            'Thử kết nối xem đúng chưa
            If GeneralClassDAL.dc.Connection.State = ConnectionState.Closed Then
                GeneralClassDAL.dc.Connection.Open()
                Me.Close()
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub btnCancel_Click(sender As System.Object, e As System.EventArgs) Handles btnCancel.Click
        Me.Close()
    End Sub

    Private Sub cmbServer_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbServer.SelectedIndexChanged
        Try
            If cmbServer.SelectedIndex >= 0 Then
                cmbDatabase.Items.Clear()
                Dim lst = CollectionHelper.GetDatabases(cmbServer.Text)
                For Each p As String In lst
                    cmbDatabase.Items.Add(p)
                Next
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
        
    End Sub

    Private Sub cmbDatabase_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbDatabase.SelectedIndexChanged

    End Sub

    Private Sub txtServer_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub cmbServer_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbServer.Leave
        Try
            LoadDatabase()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

    End Sub
End Class